<template>
  <div class="app-container">
    <!-- 添加友好提示信息 -->
    <el-card class="welcome-card">
      <div slot="header" class="card-header">
        <span><i class="el-icon-info notice-icon"></i> 温馨提示</span>
      </div>
      <div class="notice-content">
        <p>尊敬的用户，欢迎使用疫苗禁忌查询系统！</p>
        <p>在接种疫苗前，请查询相关疫苗的禁忌症及健康检查要求，确保您的安全。</p>
        <p>您可以通过<strong>疫苗名称</strong>进行搜索，了解接种前需要注意的事项。</p>
        <p class="tip">小贴士：如有任何疑问，请咨询医护人员获取专业建议。</p>
      </div>
    </el-card>

    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px" class="search-form">
      <el-form-item label="疫苗名称" prop="vaccineName">
        <el-input
          v-model="queryParams.vaccineName"
          placeholder="请输入疫苗名称"
          clearable
          @keyup.enter.native="handleQuery"
          prefix-icon="el-icon-search"
        />
      </el-form-item>
      <!-- <el-form-item label="创建时间" prop="createdAt">
        <el-date-picker clearable
          v-model="queryParams.createdAt"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择创建时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="更新时间" prop="updatedAt">
        <el-date-picker clearable
          v-model="queryParams.updatedAt"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择更新时间">
        </el-date-picker>
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:checks:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:checks:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:checks:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:checks:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-card class="table-card">
      <el-table 
        v-loading="loading" 
        :data="checksList" 
        @selection-change="handleSelectionChange"
        stripe
        border
        highlight-current-row
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="ID" align="center" prop="id" width="80" />
        <el-table-column label="疫苗名称" align="center" prop="vaccineName" min-width="120" />
        <el-table-column label="禁忌症" align="center" prop="contraindication" min-width="200" show-overflow-tooltip />
        <el-table-column label="健康检查提醒" align="center" prop="healthCheckReminder" min-width="200" show-overflow-tooltip />
        <el-table-column label="创建时间" align="center" prop="createdAt" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createdAt, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="更新时间" align="center" prop="updatedAt" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.updatedAt, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['system:checks:edit']"
            >修改</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['system:checks:remove']"
            >删除</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-view"
              @click="showDetails(scope.row)"
            >详情</el-button>
          </template>
        </el-table-column>
      </el-table>
      
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-card>

    <!-- 添加或修改疫苗信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="疫苗名称" prop="vaccineName">
          <el-input v-model="form.vaccineName" placeholder="请输入疫苗名称" />
        </el-form-item>
        <el-form-item label="禁忌症" prop="contraindication">
          <el-input v-model="form.contraindication" type="textarea" rows="4" placeholder="请输入禁忌症信息" />
        </el-form-item>
        <el-form-item label="健康检查提醒" prop="healthCheckReminder">
          <el-input v-model="form.healthCheckReminder" type="textarea" rows="4" placeholder="请输入健康检查提醒信息" />
        </el-form-item>
        <el-form-item label="创建时间" prop="createdAt">
          <el-date-picker clearable
            v-model="form.createdAt"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择创建时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="更新时间" prop="updatedAt">
          <el-date-picker clearable
            v-model="form.updatedAt"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择更新时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 详情对话框 -->
    <el-dialog title="疫苗详细信息" :visible.sync="detailsVisible" width="650px" append-to-body>
      <template v-if="detailsForm">
        <el-descriptions :column="1" border>
          <el-descriptions-item label="疫苗名称">{{ detailsForm.vaccineName }}</el-descriptions-item>
          <el-descriptions-item label="禁忌症">
            <div class="details-content">{{ detailsForm.contraindication }}</div>
          </el-descriptions-item>
          <el-descriptions-item label="健康检查提醒">
            <div class="details-content">{{ detailsForm.healthCheckReminder }}</div>
          </el-descriptions-item>
          <el-descriptions-item label="更新时间">{{ parseTime(detailsForm.updatedAt, '{y}-{m}-{d}') }}</el-descriptions-item>
        </el-descriptions>
        <div class="tips-box">
          <i class="el-icon-warning-outline"></i>
          <div class="tips-content">
            请注意：这些信息仅供参考，具体接种事宜请遵医嘱。如有疑问，请咨询专业医生。
          </div>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { listChecks, getChecks, delChecks, addChecks, updateChecks } from "@/api/system/checks";

export default {
  name: "Checks",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 疫苗禁忌查询表格数据
      checksList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 详情弹出层
      detailsVisible: false,
      // 详情表单
      detailsForm: null,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        vaccineName: null,
        contraindication: null,
        healthCheckReminder: null,
        createdAt: null,
        updatedAt: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        vaccineName: [
          { required: true, message: "疫苗名称不能为空", trigger: "blur" }
        ],
        contraindication: [
          { required: true, message: "禁忌症信息不能为空", trigger: "blur" }
        ],
        healthCheckReminder: [
          { required: true, message: "健康检查提醒不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询疫苗信息列表 */
    getList() {
      this.loading = true;
      listChecks(this.queryParams).then(response => {
        this.checksList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        vaccineName: null,
        contraindication: null,
        healthCheckReminder: null,
        createdAt: null,
        updatedAt: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加疫苗信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getChecks(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改疫苗信息";
      });
    },
    /** 查看详情操作 */
    showDetails(row) {
      this.detailsForm = row;
      this.detailsVisible = true;
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateChecks(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addChecks(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除疫苗信息编号为"' + ids + '"的数据项？').then(function() {
        return delChecks(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/checks/export', {
        ...this.queryParams
      }, `vaccines_checks_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>

<style scoped>
.welcome-card {
  margin-bottom: 20px;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.card-header {
  display: flex;
  align-items: center;
  color: #409EFF;
  font-weight: bold;
  font-size: 16px;
}

.notice-icon {
  margin-right: 8px;
  font-size: 18px;
}

.notice-content {
  padding: 10px;
  line-height: 1.6;
  color: #606266;
}

.notice-content p {
  margin: 8px 0;
}

.tip {
  color: #E6A23C;
  font-style: italic;
  margin-top: 15px;
}

.search-form {
  margin-bottom: 15px;
  background-color: #f9f9f9;
  padding: 15px;
  border-radius: 8px;
}

.table-card {
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.details-content {
  white-space: pre-wrap;
  line-height: 1.6;
  padding: 5px;
}

.tips-box {
  margin-top: 20px;
  display: flex;
  align-items: flex-start;
  background-color: #fdf6ec;
  padding: 15px;
  border-radius: 5px;
  border-left: 4px solid #E6A23C;
}

.el-icon-warning-outline {
  color: #E6A23C;
  font-size: 18px;
  margin-right: 10px;
  margin-top: 2px;
}

.tips-content {
  flex: 1;
  color: #7d6b3e;
  font-size: 14px;
}
</style>
